using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
using Quartz;
using Castle.Facilities.Startable;
using Castle.Facilities.QuartzIntegration;
using MvcCms.Scheduler;
using System;
using System.Linq;
using MvcCms.Service.Scheduler;
namespace MvcCms.Scheduler.Installer
{
    public class InstallAndRegisterJobs : IWindsorInstaller, IInstallAndRegisterJobs
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container
                .Register(AllTypes.FromThisAssembly()
                .BasedOn<IJob>()
                .WithService.Self())
                .AddFacility<StartableFacility>()
                .AddFacility<QuartzFacility>("quartz");

            //var jobScheduler = container.Resolve<IScheduler>();
            //var newJobTrigger = TriggerUtils.MakeSecondlyTrigger(10);
            //var jobType = typeof(SampleJob);
            //var name = jobType.Name;
            //newJobTrigger.Name = name + " trigger";
            //jobScheduler.ScheduleJob(new JobDetail(name, jobType), newJobTrigger);

            //get all other assemblies that implement the IInstallAndRegisterJobs interface and call install on them
            //will need to give this project a reference to them and register them with windsor here
            //but other than that no code sharing
            //container.Register(AllTypes.FromAssemblyNamed("Your.Scheduler")
            //                    .Where(type => type.Name.EndsWith("RegisterJobs"))
            //                    .WithService.DefaultInterface()
            //                    .Configure(c => c.LifeStyle.Transient));

            var scheduleInstallers = container.ResolveAll<IInstallAndRegisterJobs>();
            foreach (var scheduleInstaller in scheduleInstallers)
            {
                scheduleInstaller.Install(container, store);                
            }
        }
    }
}